<template>
  <div class="login-container">
    <h1>登录</h1>
    <form @submit.prevent="handleLogin">
      <div class="form-group">
        <label for="username">用户名(23007591)</label>
        <input 
          id="username" 
          v-model="form.username" 
          type="text" 
          placeholder="请输入学号"
          required
        >
      </div>
      <div class="form-group">
        <label for="password">密码(123456)</label>
        <input 
          id="password" 
          v-model="form.password" 
          type="password" 
          placeholder="请输入密码"
          required
        >
      </div>
      <button type="submit" class="login-btn">登录</button>
      <button type="button" class="demo-btn" @click="fillDemoAccount">
        使用演示账号登录
      </button>
    </form>
    <p class="register-link">
      还没有账号？<router-link to="/register">立即注册</router-link>
    </p>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../stores/auth'
import api from '../composables/useApi'

const form = ref({
  username: '',
  password: ''
})

const router = useRouter()
const authStore = useAuthStore()

const fillDemoAccount = () => {
  form.value = {
    username: '23007591', // 替换为你的学号
    password: '123456'
  }
}

const handleLogin = async () => {
  try {
    const response = await api.login(form.value)
    authStore.login(response.data.user, response.data.token)
    router.push('/movies')
  } catch (error) {
    alert('登录失败: ' + error.response?.data?.message || error.message)
  }
}
</script>

<style scoped>
.login-container {
  max-width: 400px;
  margin: 2rem auto;
  padding: 2rem;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

h1 {
  text-align: center;
  margin-bottom: 1.5rem;
  color: #333;
}

.form-group {
  margin-bottom: 1rem;
}

label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: bold;
}

input {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
}

button {
  width: 100%;
  padding: 0.75rem;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  cursor: pointer;
  margin-top: 0.5rem;
}

.login-btn {
  background-color: #42b983;
  color: white;
}

.demo-btn {
  background-color: #f0ad4e;
  color: white;
}

.register-link {
  text-align: center;
  margin-top: 1rem;
}

a {
  color: #42b983;
  text-decoration: none;
}
</style>